<template>
	<view>
		<uni-forms style="margin-top: 10rpx;">
			<uni-forms-item label="收货人">
				<uni-easyinput class="input" placeholder="请填写收货人姓名" v-model="newAddr.addressName"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item label="联系手机">
				<uni-easyinput class="input" placeholder="请填写联系手机" v-model="newAddr.addressTel"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item label="所在地区">
				<uni-easyinput class="input" placeholder="请选择" @focus="pickAddr()"
					:value="this.newAddr.province+' '+this.newAddr.city+' '+this.newAddr.county"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item label="详细地址">
				<uni-easyinput class="input" placeholder="请填写详细地址" v-model="newAddr.detail"></uni-easyinput>
			</uni-forms-item>
			<uni-forms-item label="设为默认">
				<switch @change="moren"></switch>
			</uni-forms-item>
		</uni-forms>
		<view>
			<button style="background-color: #00d583;color: white;width: 700rpx;border-radius: 30rpx;"
				@click="addAddr()">
				<!-- <uni-icons type="plusempty" style="color: white;font-weight: bold;" size="30rpx"></uni-icons> -->
				保存地址
			</button>
		</view>
		<view>
			<pickAddress ref="pickAdress" @addr="dataFromPickAddress"></pickAddress>
		</view>
	</view>
</template>

<script>
	import pickAddress from '@/components/john-pickAddress/pickAddress.vue'
	export default {
		data() {
			return {
				newAddr:{
					province: '', //省
					city: '', //市
					county: '', //区,
					defaultAddress: 0,
					addressName:"",
					addressTel:"",
					detail:""
				}
				

			}
		},
		components: {
			pickAddress
		},
		methods: {
			addAddr(){
				uni.request({
					url:'api/custom-server/addAddr',
					method:'POST',
					data: this.newAddr,
					header: {
						"token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ6eU1hbmFnZSI6eyJpZCI6IjE4MjA3NzkzMTgxODg1NzY3NjgiLCJtYW5hZ2VBY2NvdW50IjoiY2oiLCJtYW5hZ2VOaWNrTmFtZSI6ImNqIn19.AI1W3jxpA1T1k438YI8weWTw_51jKU2uUQAjKKJ3Rig"
					},
					success: (res) => {
						if(res.data.code == 200){
							uni.showToast({
							  title: '添加成功',
							  icon: 'success',
							  duration: 2000
							});
							setTimeout(function(){
								uni.reLaunch({
									url:'/pages/myAddr/myAddr'
								})
							},1500)
						}
					}
				})
			},
			pickAddr() {
				this.$refs.pickAdress.onOpen()
			},
			dataFromPickAddress(e) {
				this.newAddr.province = e.province;
				this.newAddr.city = e.city;
				this.newAddr.county = e.county;
				// console.log(this.province)
			},
			moren(e) {
				if (e.detail.value == true) {
					this.newAddr.defaultAddress = 1;
				}else{
					this.newAddr.defaultAddress = 0;
				}
				
			}
		},
		onLoad() {

		}
	}
</script>

<style>
	.input {
		width: 85%;
	}
</style>